# coding=utf-8
from Page.base_page import BasePage


class ProcessSearch(BasePage):

    def _get_search_result(self):
        """
        获取搜索结果
        :return:
        """
        # 等待搜索结果出来
        self.wait_for_visible('css', '.layui-table-body.layui-table-main>table tr')
        search_count = len(self.finds('css', '.layui-table-body.layui-table-main>table tr'))
        self.log.info(f"查询结果:{search_count}")
        return search_count

    def _select_auditor(self, auditor, frame_xpath):
        """
        选择责任人通用方法
        :param auditor: 要搜索的责任人
        :param frame_xpath: 切换frame的xpath定位
        :return:
        """
        self.until_switch_frame_success('xpath', frame_xpath)
        # 输入责任人code或姓名
        self.until_input_success('css', '#selectProcessAuditors .tt-input', value=auditor)
        # 点击搜索结果
        self.until_click_success('css', "#selectProcessAuditors .tt-suggestion.tt-selectable")
        # 切回主路径
        self.driver.switch_to.default_content()
        # 选择责任人弹窗点击确定
        self.until_click_success('css', ".layui-layer-btn0")

    def by_process_search(self, process_name):
        """
        通过流程名称搜索
        :param process_name:
        :return:
        """
        self.input('id', 'processName', process_name)
        self.click('id', 'advancedSearchBtn')
        result = self._get_search_result()
        return result

    def by_initiator_search(self, initiator):
        """
        通过发起人搜索
        :param initiator:
        :return:
        """
        self.click('css', '#js-selectInitiatePerson .wrap-selectAuditors.cf')
        self._select_auditor(initiator, '//iframe[contains(@src,"/backend/publicpage/SelectAuditorsCenter")]')
        # 点击搜索按钮
        self.until_click_success('css', "#advancedSearchBtn")
        result = self._get_search_result()
        return result

    def by_form_keywords_search(self, keywords):
        """
        通过表单关键字搜索
        :param keywords:
        :return:
        """

        # 输入表单关键字，然后搜索
        self.until_input_success('id', 'formKeyWords', value=keywords)
        self.click('id', 'advancedSearchBtn')
        result = self._get_search_result()
        return result

    def by_approve_person_search(self, approve_person):
        """
        通过审批人搜索
        :param approve_person:
        :return:
        """
        # 点击审批人输入框
        self.click('css', '#js-selectApprovePerson .wrap-selectAuditors.cf')
        self._select_auditor(approve_person, '//iframe[contains(@src,"/backend/publicpage/SelectAuditorsCenter")]')
        # 点击搜索按钮
        self.until_click_success('css', "#advancedSearchBtn")
        result = self._get_search_result()
        return result

    def by_initiate_datetime_search(self, start_time, end_time):
        """
        通过发起时间搜索
        :param start_time:
        :param end_time:
        :return:
        """
        # 输入发起时间的开始和结束
        self.input_start_end_time("#initiateDateTimeStart", start_time, "#initiateDateTimeEnd", end_time)
        # 点击搜索按钮
        self.until_click_success('css', "#advancedSearchBtn")
        result = self._get_search_result()
        return result

    def by_process_status_search(self, status):
        """
        通过流程状态搜索
        :param status: 流程状态
        :return:
        """
        self.select('#select-processStatus', f'#popup-processStatus>li[data-text="{status}"]')
        # 点击搜索按钮
        self.until_click_success('css', "#advancedSearchBtn")
        result = self._get_search_result()
        return result

    def by_current_auditor_search(self, auditor):
        """
        通过当前责任人搜索
        :param auditor: 责任人
        :return:
        """
        # 点击当前责任人输入框
        self.click('css', '#js-selectHandlePerson .wrap-selectAuditors.cf')
        self._select_auditor(auditor, '//iframe[contains(@src,"/backend/publicpage/SelectAuditorsCenter")]')
        # 点击搜索按钮
        self.until_click_success('css', "#advancedSearchBtn")
        result = self._get_search_result()
        return result

    def by_finish_datetime_search(self, start_time, end_time):
        """
        通过结束时间搜索
        :param start_time:
        :param end_time:
        :return:
        """
        # 输入发起时间的开始和结束
        self.input_start_end_time("#finishDateTimeStart", start_time, "#finishDateTimeEnd", end_time)
        # 点击搜索按钮
        self.until_click_success('css', "#advancedSearchBtn")
        result = self._get_search_result()
        return result

    def by_business_type_search(self, business_type):
        """
        通过业务类型搜索
        :param business_type:
        :return:
        """
        # 选择业务类型
        self.click('css', '#selProcessStatus')
        self.until_switch_frame_success('xpath', '//iframe[contains(@src,"/backend/publicpage/treegrid-select.aspx")]')
        self.until_input_success('id', 'Name', value=business_type)
        # import time
        # time.sleep(2)
        self.until_click_success('id', 'easy_search_btn')
        self.click('xpath', f'//*[@id="module_tree"]//a[text()="{business_type}"]')
        self.driver.switch_to.default_content()
        self.click('css', '.layui-layer-btn0')
        # 点击搜索按钮
        self.until_click_success('css', "#advancedSearchBtn")
        result = self._get_search_result()
        return result

    def by_company_name_search(self, company):
        """
        通过公司名称搜索
        :param company:
        :return:
        """
        # 输入表单关键字，然后搜索
        self.until_input_success('id', 'buname', value=company)
        self.click('id', 'advancedSearchBtn')
        result = self._get_search_result()
        return result

    def fold_or_unfold_advanced_search_box(self):
        """
        收起或展开查询条件
        :return: 返回
        """
        self.until_click_success('css', 'span#searchToggle')
        text = self.find('css', 'span#searchToggle').text
        style = self.find('css', 'div.search-table').get_attribute('style')
        return text, style
